/****************************************************************************
	This file is part of Basqet.

	Basqet is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Basqet is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
    along with Basqet.  If not, see <http://www.gnu.org/licenses/>.

****************************************************************************/

#include "notepadpage.h"
#include "ui_notepadpage.h"

#include "absolutelayout.h"
#include "pagenotewidget.h"

#include "basketcontent.h"

#include "notepadpagedata.h"

#include "eventfilterundoredo.h"

#include "commandmanager.h"
#include "cmdcreatenewtextnote.h"

#include "tagmanager.h"

#include <QMouseEvent>
#include <QGridLayout>
#include <QSizePolicy>

#include <QTextEdit>
#include <QtDebug>



NotePadPage::NotePadPage(QExplicitlySharedDataPointer<BasketContent> basketContent,
						 QExplicitlySharedDataPointer<CommandManager> commandManager,
						 QExplicitlySharedDataPointer<TagManager> tagManager,
						 QWidget *parent) :
    QWidget(parent),
	m_ui(new Ui::NotePadPage),
	mBasketContent(basketContent),
	mCommandManager(commandManager),
	mTagManager(tagManager),
	mActivateNotes(false)
{
    m_ui->setupUi(this);

	setBackgroundRole(QPalette::Base);

	setLayout(new AbsoluteLayout(this, size()));

	connect (this, SIGNAL(Clicked(QPoint)), this, SLOT(createNoteEvent(QPoint)));

	connect(mBasketContent.data(), SIGNAL(noteDataCreated(QExplicitlySharedDataPointer<NoteData>)), this, SLOT(createNoteWidget(QExplicitlySharedDataPointer<NoteData>)));

	installUndoRedoHandlers(this);

    qDebug() << "Desktop Size: " << size();

}

NotePadPage::~NotePadPage()
{
    delete m_ui;
}

void NotePadPage::installUndoRedoHandlers(QObject* target)
{
	EventFilterUndoRedo*	filter = new EventFilterUndoRedo();
	filter->installIn(target, mCommandManager.data());

}

void NotePadPage::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void NotePadPage::mousePressEvent(QMouseEvent* event)
{

	if (mActiveNote)
	{
		qDebug() << "NotPadPAge::mousePress - deactivate!";
		emit deActivated(mActiveNote.data(), UICapabilities());
		mActiveNote = 0;
	}

	QWidget::mousePressEvent(event);

}

void NotePadPage::enterEvent(QEvent* event)
{
	if (mActiveNote)
	{
		mActiveNote->setFocus();
	}


	QWidget::enterEvent(event);

}


void NotePadPage::mouseDoubleClickEvent(QMouseEvent *event)
{
	emit Clicked(event->pos());
}

void NotePadPage::createNoteEvent(QPoint where)
{
	mActivateNotes = true;
	CmdCreateNewTextNote* command = new CmdCreateNewTextNote(where, mBasketContent);
	mCommandManager->Push(command);
	mActivateNotes = false;

}

void NotePadPage::createNoteWidget(QExplicitlySharedDataPointer<NoteData> noteData)
{

	qDebug() << "Create notewidget for note" << noteData->getNoteID();
	PageNoteWidget* note = new PageNoteWidget(noteData, mCommandManager, mTagManager, this);

	connect(note, SIGNAL(activated(QObject*,UICapabilities)), this, SLOT(objectActivated(QObject*,UICapabilities)));

	layout()->addWidget(note);

	if (mActivateNotes)
		note->activateInput();

}

void NotePadPage::objectActivated(QObject *object, UICapabilities capabilities)
{

	if (mActiveNote != object)
	{
		if (mActiveNote)
		{
			emit deActivated(mActiveNote.data(), mActiveNoteCan);
		}

		mActiveNote = dynamic_cast<QWidget *>(object);

		if (!mActiveNote)
		{
			qDebug() << "Activating NULL-object";
		}

		if (!mActiveNote && object)
		{
			qDebug() << "Problem: Cannot cast active object to a widget";
		}

		if (mActiveNote)
		{
			mActiveNoteCan = capabilities;
			emit activated(object, capabilities);
		}

	}

}

